< previous page page_449 next page >

Page 449
error handler isn't enabled or there is none coded for the routine, Visual Basic will search backward through the pending procedures in the calls list. First it will look at Function B, then Subroutine A, then the initial Click event looking for an enabled error-handling routine. The search will go no further back than the top of the current calls list. If it doesn't find any enabled error handler, Visual Basic will present the default error message and halt execution of the application. If an enabled error-handling routine is found, execution will continue in that routine as if the error occurred in the same procedure that contains the error handler itself.
If you haven't caught up to the error yet, here's the problem with all of this. If a Resume or Resume Next statement is executed in the error-handling routine, control will be returned as described in the following list:
ResumeThe call to the procedure that was just searched is re-executed.
Resume NextExecution will return to the statement following the last statement that was executed in the current routine (the one that contains the error-handling routine).
The common thread here is that the statement that will be executed is in the routine where the error-handling routine was found, not necessarily in the routine where the error occurred. If you don't remember this, and don't code an error handler in every routine that you create, your code may perform in ways that you did not intend it to. Also, if the error routine cannot handle the error that occurred, you should use the Raise method to generate an error that will be handled by the error-handling routine in the calling procedure.
Handling Errors
When you have decided how you want to handle the errors that you are anticipating in the routines of your application, you can start writing the actual error-handling code. The first step in any error routine is to add a line label to mark the beginning of the error-handling routine. You should use a descriptive name for the label and it should be followed by a colon (:). The next step is to code the body of the error-handling routine. This is the code that actually handles the error, usually by using a Select statement to check for the error codes that are likely to occur in this procedure, and specifying the action to be taken for each of them. In addition, you should add the else statement to the Select, just in case an error has occurred that you did not anticipate.

 
< previous page page_449 next page >

If you like this book, buy it!